package com.xcw.jac;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xcw
 * @date 2023/7/3 14:59
 * @description
 */
public class jac {

    public static void main(String[] args) throws IOException {
//        List<String> list = new jac().fourSum("23");
//        list.stream().forEach(s -> {
//            System.out.println(s);
//        });
        System.out.println(new jac().fourSum(new int[]{1, 0, -1, 0, -2, 2}, 0));
    }

        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> arr = new ArrayList<>();
            if (nums.length <= 4 || nums == null){
                return arr;
            }
            Arrays.sort(nums);
            int[] tab = new int[4];
            tab[0]= nums[0];
            tab[1]= nums[1];
            tab[2]= nums[2];
            tab[3]= nums[3];
            List<int[]> arr2 = new ArrayList<>();
            bark(tab, arr2 , nums, 0, target);
            arr = arr2.stream().map(ints -> Arrays.stream(ints).boxed().collect(Collectors.toList())).collect(Collectors.toList());
            return arr;
        }

    private void bark(int[] tab, List<int[]> arr, int[] nums, int index, int target) {
        if (index > 3) {
            return;
        }
        int temp = tab[index];
        for (int i = 4; i < nums.length; i++) {
            tab[index] = nums[i];
            if (sum(tab) == target){
                int[] ints = Arrays.copyOf(tab, 4);
                Arrays.sort(ints);
                arr.add(ints);
            }

        }
        tab[index] = temp;
        index++;
        bark(tab, arr,nums,index,target);

    }

    private int sum(int[] tab) {
        int sum = 0;
        for (int i = 0; i < tab.length; i++) {
            sum += tab[i];
        }
        return sum;
    }
}
